package com.xiaoshuidi.cloud.module.iot.controller.admin.account;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.iot.common.ResultModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.account.AddThirdApiAccountVo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbAccountDo;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.enums.ErrorCodeEnum;
import com.xiaoshuidi.cloud.module.iot.service.MeChantAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author arvin
 * @date 2019-04-10 09:01
 * @description
 */
@RestController
@Tag(name = "智能设备 - 设备账号")
@RequestMapping(ApiConstants.NAME + "/mechant")
public class MeChantAccountController {

    private final static Logger LOGGER = LoggerFactory.getLogger(MeChantAccountController.class);

    @Autowired
    private MeChantAccountService meChantAccountService;


    /**
     * 添加品牌对应的第三方API账号
     *
     * @param tbAccountDo
     * @return
     */
    @Operation(summary = "添加品牌对应的第三方API账号")
    @Parameter(name = "tbAccountDo", description = "账户实体类")
    @PostMapping("/brand/addThirdApi")
    public ResultModel<AddThirdApiAccountVo> addThirdApiAccount(@RequestBody TbAccountDo tbAccountDo) {
        ResultModel<AddThirdApiAccountVo> addThirdApiAccountVoResultModel = null;
        try {
            addThirdApiAccountVoResultModel = meChantAccountService.addThirdApiAccount(tbAccountDo);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("account->新增品牌对应的第三方API账号,请求参数:{},异常原因:{}", JSONObject.toJSONString(tbAccountDo), e.getMessage());
            addThirdApiAccountVoResultModel = ResultModel.createNativeFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getMsg());
        }
        return addThirdApiAccountVoResultModel;
    }

    /**
     * 根据apiNo查询tb_account记录
     *
     * @param apiNo
     * @return
     */
    @Operation(summary = "根据apiNo查询tb_account记录")
    @Parameter(name = "apiNo", description = "品牌编号")
    @GetMapping("/brand/getThirdApiByApiNo")
    public ResultModel<TbAccountDo> getThirdApiByApiNo(@RequestParam("apiNo") String apiNo) {
        ResultModel<TbAccountDo> tbAccountModelResultModel = null;
        try {
            tbAccountModelResultModel = meChantAccountService.getThirdApiByApiNo(apiNo);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("account->根据apiNo查询tb_account记录,请求参数:{},异常原因:{}", apiNo, e.getMessage());
            tbAccountModelResultModel = ResultModel.createNativeFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getMsg());
        }
        return tbAccountModelResultModel;
    }

    /**
     * 删除品牌对应的第三方API账号
     *
     * @param tbAccountModel
     * @return
     */
    @Operation(summary = "删除品牌对应的第三方API账号")
    @Parameter(name = "tbAccountModel", description = "账户实体类")
    @PostMapping("/brand/deleteThirdApi")
    public ResultModel deleteThirdApiAccount(@RequestBody TbAccountDo tbAccountModel) {
        ResultModel resultModel = null;
        try {
            resultModel = meChantAccountService.deleteThirdApiAccount(tbAccountModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("account->删除品牌对应的第三方API账号,请求参数:{},异常原因:{}", JSONObject.toJSONString(tbAccountModel), e.getMessage());
            resultModel = ResultModel.createNativeFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getMsg());
        }
        return resultModel;
    }

    /**
     * 修改品牌对应的第三方API账号
     *
     * @param tbAccountModel
     * @return
     */
    @Operation(summary = "修改品牌对应的第三方API账号")
    @Parameter(name = "tbAccountModel", description = "账户实体类")
    @PostMapping("/brand/updateThirdApi")
    public ResultModel updateThirdApiAccount(@RequestBody TbAccountDo tbAccountModel) {
        ResultModel resultModel = null;
        try {
            resultModel = meChantAccountService.updateThirdApiAccount(tbAccountModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("account->修改品牌对应的第三方API账号,请求参数:{},异常原因:{}", JSONObject.toJSONString(tbAccountModel), e.getMessage());
            resultModel = ResultModel.createNativeFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getMsg());
        }
        return resultModel;
    }


    /**
     * 获取设备平台账号
     *
     * @return
     */
    @Operation(summary = "获取设备平台账号")
    @Parameters({
            @Parameter(name = "pageSize", description = "分页大小"),
            @Parameter(name = "pageNum", description = "分页页数")
    })
    @GetMapping("/brand/deviceAccount")
    public ResultModel<PageInfo<TbAccountDo>> getDeviceAccount(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum) {
        ResultModel<PageInfo<TbAccountDo>> resultModel = null;

        try {
            resultModel = meChantAccountService.getDeviceAccount(SecurityFrameworkUtils.getLoginUserId(), pageSize, pageNum);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("account->获取设备平台账号,请求参数:{},异常原因:{}", SecurityFrameworkUtils.getLoginUserId(), e.getMessage());
            resultModel = ResultModel.createNativeFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getMsg());
        }
        return resultModel;

    }

}
